package org.xt.module.api;

import java.util.Date;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.xt.constants.Res;
import org.xt.entity.DataDetailsHM;
@IocBean
@At("/api/dataDetails/hm")
public class DataDetailsHMApiModule {
	@Inject
	private Dao dao;
	
	@At("/list")
	@Ok("json")
	public Res list(@Param(value="page", df="1")int page, @Param(value="size", df="10")int size
			, @Param(value="machine", df="")String machine
			, @Param(value="item", df="")String item
			, @Param(value="gjehState", df="")String gjehState
			, @Param(value="tempputState", df="")String tempputState
			, @Param(value="liaohao", df="")String liaohao
			, @Param(value="gjNo", df="")String gjNo
			, @Param(value="daChuwei", df="")String daChuwei
			, @Param(value="xiaoChuwei", df="")String xiaoChuwei){
		Pager pager = dao.createPager(page, size);
		Cnd cnd = Cnd.where("dataMachine", "like", "%" + machine + "%")
				.and("dataItem", "like", "%" + item + "%")
				.and("dataGJEHState", "like", "%" + gjehState + "%")
				.and("dataTempputState", "like", "%" + tempputState + "%")
				.and("liaohao", "like", "%" + liaohao + "%")
				.and("gjNo", "like", "%" + gjNo + "%")
				.and("daChuwei", "like", "%" + daChuwei + "%")
				.and("xiaoChuwei", "like", "%" + xiaoChuwei + "%");
		pager.setRecordCount(dao.count(DataDetailsHM.class, cnd));
		String strsql = "SELECT SUM(NUM) AS total FROM tb_data_1fgjeh_details_hm  WHERE "
				+ " dataMachine LIKE @dataMachine "
				+ " AND dataItem LIKE @dataItem "
				+ " AND dataGJEHState LIKE @dataGJEHState "
				+ " AND dataTempputState LIKE @dataTempputState "
				+ " AND liaohao LIKE @liaohao "
				+ " AND gjNo LIKE @gjNo "
				+ " AND daChuwei LIKE @daChuwei "
				+ " AND xiaoChuwei LIKE @xiaoChuwei";
		Sql sql = Sqls.create(strsql);
		sql.params().set("dataMachine", "%" + machine + "%").set("dataItem", "%" + item + "%")
			.set("dataGJEHState", "%" + gjehState + "%").set("dataTempputState", "%" + tempputState + "%")
			.set("liaohao", "%" + liaohao + "%")
			.set("gjNo", "%" + gjNo + "%").set("daChuwei", "%" + daChuwei + "%").set("xiaoChuwei", "%" + xiaoChuwei + "%");
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao.getEntity(Record.class));
		dao.execute(sql);
		List<Record> sums = sql.getList(Record.class);
		int total  = 0;
		if(sums != null && sums.size() > 0){
			Record sum = sums.get(0);
			total = sum.getInt("total");
		}
		
		List<DataDetailsHM> list = dao.query(DataDetailsHM.class, cnd, pager);
		return Res.NEW().code(Res.ERROR).msg("Total: " + total).data(new QueryResult(list, pager));
	}
	@At("/in")
	@Ok("json")
	public Res in(@Param("..")DataDetailsHM details){
		DataDetailsHM dbDetils = dao.fetch(DataDetailsHM.class
				,  Cnd.where("dataMachine", "=", details.getDataMachine())
					.and("dataItem", "=", details.getDataItem())
					.and("dataGJEHState", "=", details.getDataGJEHState())
					.and("dataTempputState", "=", details.getDataTempputState())
					.and("gjNo", "=", details.getGjNo())
					.and("daChuwei", "=", details.getDaChuwei())
					.and("liaohao", "=", details.getLiaohao())
					.and("xiaoChuwei", "=", details.getXiaoChuwei()));
		
		if(dbDetils != null){
			details.setId(dbDetils.getId());
			details.setUpdateTime(new Date());
			details.setNum(details.getNum() + dbDetils.getNum());
			if(dao.updateIgnoreNull(details) > 0){
				return Res.NEW().code(Res.SUCCESS).msg("In 手动登入成功");
			}else{
				return Res.NEW().code(Res.ERROR).msg("In 手动登入失败");
			}
		}else{
			details.setCreateTime(new Date());
			details.setUpdateTime(new Date());
			details = dao.insert(details);
			if(details != null){
				return Res.NEW().code(Res.SUCCESS).msg("In 手动登入成功");
			}else{
				return Res.NEW().code(Res.ERROR).msg("In 手动登入失败");
			}
		}
	}
	
	@At("/out")
	@Ok("json")
	public Res out(@Param("..")DataDetailsHM details){
		DataDetailsHM dbDetils = dao.fetch(DataDetailsHM.class
				,  Cnd.where("dataMachine", "=", details.getDataMachine())
					.and("dataItem", "=", details.getDataItem())
					.and("dataGJEHState", "=", details.getDataGJEHState())
					.and("dataTempputState", "=", details.getDataTempputState())
					.and("gjNo", "=", details.getGjNo())
					.and("liaohao", "=", details.getLiaohao())
					.and("daChuwei", "=", details.getDaChuwei())
					.and("xiaoChuwei", "=", details.getXiaoChuwei()));
		
		if(dbDetils != null){
			details.setId(dbDetils.getId());
			details.setUpdateTime(new Date());
			details.setNum(dbDetils.getNum() - details.getNum());
			if(details.getNum() > 0){
				dao.update(details);
			}else{
				dao.delete(dbDetils);
			}
		}
		return Res.NEW().code(Res.SUCCESS).msg("OUT 手动登出成功");
	}
}
